<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  
  <title>题解 洛谷 P1763 埃及分数 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta name="description" content="Java是真的慢">
<meta property="og:type" content="article">
<meta property="og:title" content="题解 洛谷 P1763 埃及分数">
<meta property="og:url" content="http://example.com/2022/02/14/%E9%A2%98%E8%A7%A3%20%E6%B4%9B%E8%B0%B7%20P1763%20%E5%9F%83%E5%8F%8A%E5%88%86%E6%95%B0/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="Java是真的慢">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2022-02-14T09:37:00.850Z">
<meta property="article:modified_time" content="2023-07-19T11:50:47.298Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
  
    <link rel="alternate" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="shortcut icon" href="/favicon.png">
  
  
    
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/typeface-source-code-pro@0.0.71/index.min.css">

  
  
<link rel="stylesheet" href="/css/style.css">

  
    
<link rel="stylesheet" href="/fancybox/jquery.fancybox.min.css">

  
  
<meta name="generator" content="Hexo 6.3.0"></head>

<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"><span class="fa fa-bars"></span></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
        
          <a class="nav-icon" href="/atom.xml" title="RSS Feed"><span class="fa fa-rss"></span></a>
        
        <a class="nav-icon nav-search-btn" title="Search"><span class="fa fa-search"></span></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://example.com"></form>
      </div>
    </div>
  </div>
</header>

      <div class="outer">
        <section id="main"><article id="post-题解 洛谷 P1763 埃及分数" class="h-entry article article-type-post" itemprop="blogPost" itemscope itemtype="https://schema.org/BlogPosting">
  <div class="article-meta">
    <a href="/2022/02/14/%E9%A2%98%E8%A7%A3%20%E6%B4%9B%E8%B0%B7%20P1763%20%E5%9F%83%E5%8F%8A%E5%88%86%E6%95%B0/" class="article-date">
  <time class="dt-published" datetime="2022-02-14T09:37:00.850Z" itemprop="datePublished">2022-02-14</time>
</a>
    
  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="p-name article-title" itemprop="headline name">
      题解 洛谷 P1763 埃及分数
    </h1>
  

      </header>
    
    <div class="e-content article-entry" itemprop="articleBody">
      
        <p>Java是真的慢</p>
<p>这么经典的题目居然这么少人写题解，那么我这个蒟蒻就来写一发 <code>Java</code> 题解罢！</p>
<p>由于这道题的搜索树深度无限，所以排除掉深搜；每层能扩展的广度也无限，所以排除广搜。那么，我们就只能使用一种深搜广搜结合的算法——迭代加深搜索。迭代加深搜索就是不断放宽搜索树最大深度的深搜。</p>
<p>我们还需要一些剪枝优化。</p>
<ul>
<li>优化1：为了避免重复搜索，我们强制让分母递增。</li>
<li>优化2：由于分母递增，那么如果当前搜索的分数过小，在搜索树深度有限的情况下（这也是使用迭代加深的原因之一）就永远凑不够数，所以可以剪枝。这个优化可以把无限的扩展宽度剪成有限的，非常强力。</li>
<li>优化3：由于 <code>Java</code> 巨大的常数，所以可以卡时，如果快要超时就直接输出。</li>
</ul>
<p>```java<br>import java.util.Scanner;</p>
<p>class frac{//一个支持加减法和比较的分数类<br>    long a,b;<br>    long gcd(long x,long y){<br>        if(y==0)return x;<br>        else return gcd(y,x%y);<br>    }<br>    frac(){a=0;b=1;}<br>    frac(long _a,long _b){a=_a;b=_b;yuefen();}<br>    void yuefen(){<br>        long d=gcd(a,b);<br>        a/=d;b/=d;<br>    }<br>    frac plus(frac r){<br>        frac res=new frac(a<em>r.b+r.a</em>b,b<em>r.b);<br>        res.yuefen();<br>        return res;<br>    }<br>    frac sub(frac r){<br>        frac res=new frac(a</em>r.b-r.a<em>b,b</em>r.b);<br>        res.yuefen();<br>        return res;<br>    }<br>    long cmp(frac r){<br>        frac t1=new frac(a<em>r.b,b</em>r.b),t2=new frac(r.a<em>b,b</em>r.b);<br>        return t1.a-t2.a;<br>    }<br>    boolean same(frac r){<br>        yuefen();r.yuefen();<br>        return a==r.a&amp;&amp;b==r.b;<br>    }<br>}</p>
<p>public class Main{<br>    static frac to;<br>    static long[] s=new long[114514],best=new long[114514];<br>    static boolean flag=false;<br>    static long clk;<br>    static void dfs(int step,int maxstep,frac now){<br>        if(++clk&gt;1000000){//卡时<br>            return;<br>        }<br>        if(step&gt;maxstep){<br>            if(now.same(to)){<br>                if(best[1]==0||s[step-1]&lt;best[step-1]){//找最优解<br>                    for(int i=1;i&lt;=step;i++){<br>                        best[i]=s[i];<br>                    }<br>                    flag=true;<br>                }<br>            }<br>            return;<br>        }<br>        long pre=s[step-1],res=maxstep-step+1;<br>        frac tmp=to.sub(now);<br>        for(long i=Math.max(pre+1,tmp.b/tmp.a)/<em>优化1</em>/;i&lt;=tmp.b<em>res/tmp.a/</em>优化2*/;i++){<br>            s[step]=i;<br>            dfs(step+1,maxstep,now.plus(new frac(1,i)));<br>        }<br>    }<br>    public static void main(String[] args){<br>        Scanner input=new Scanner(System.in);<br>        to=new frac();<br>        to.a=input.nextLong();to.b=input.nextLong();<br>        int i=0;<br>        s[0]=1;<br>        while(!flag){//迭代加深<br>            i++;<br>            dfs(1,i,new frac(0,1));<br>        }<br>        for(int j=1;j&lt;=i;j++){<br>            System.out.printf(“%d “,best[j]);<br>        }<br>        input.close();<br>    }<br>}</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://example.com/2022/02/14/%E9%A2%98%E8%A7%A3%20%E6%B4%9B%E8%B0%B7%20P1763%20%E5%9F%83%E5%8F%8A%E5%88%86%E6%95%B0/" data-id="clk9o8lnb000n8wi5di33c5ds" data-title="题解 洛谷 P1763 埃及分数" class="article-share-link"><span class="fa fa-share">Share</span></a>
      
      
      
    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2022/02/17/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20%E7%BD%91%E7%BB%9C%E6%B5%81/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          学习笔记 网络流
        
      </div>
    </a>
  
  
    <a href="/2022/02/11/%E4%B8%BB%E5%B8%AD%E6%A0%91%E3%80%81%E5%87%BD%E6%95%B0%E5%BC%8F%E7%BA%BF%E6%AE%B5%E6%A0%91%E3%80%81%E5%8F%AF%E6%8C%81%E4%B9%85%E5%8C%96%E7%BA%BF%E6%AE%B5%E6%A0%91%20%E5%88%9D%E6%AD%A5%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">主席树、函数式线段树、可持久化线段树 初步 学习笔记</div>
    </a>
  
</nav>

  
</article>


</section>
        
          <aside id="sidebar">
  
    

  
    

  
    
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2023/07/">July 2023</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/10/">October 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/05/">May 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/02/">February 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2022/01/">January 2022</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/08/">August 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/07/">July 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2021/06/">June 2021</a></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2020/08/">August 2020</a></li></ul>
    </div>
  </div>


  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2023/07/17/%E5%86%B3%E7%AD%96%E5%8D%95%E8%B0%83%E6%80%A7%E4%BC%98%E5%8C%96DP%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%20AND%20P4767%20%E3%80%8CIOI2000%E3%80%8D%20%E9%82%AE%E5%B1%80%20%E9%A2%98%E8%A7%A3/">决策单调性优化DP 学习笔记 AND P4767 「IOI2000」 邮局 题解</a>
          </li>
        
          <li>
            <a href="/2022/10/30/CSP-S%202022%20%E6%B8%B8%E8%AE%B0/">CSP-S 2022 游记</a>
          </li>
        
          <li>
            <a href="/2022/05/29/%E6%96%9C%E7%8E%87%E4%BC%98%E5%8C%96%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%20%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/">斜率优化动态规划 学习笔记</a>
          </li>
        
          <li>
            <a href="/2022/05/22/PKUSC%E4%B8%80%E5%8F%A5%E8%AF%9D%E6%B8%B8%E8%AE%B0/">PKUSC一句话游记</a>
          </li>
        
          <li>
            <a href="/2022/05/18/%E7%9C%81%E9%80%89%E6%B8%B8%E8%AE%B0%EF%BC%9F%E7%9C%81%E9%80%89%E6%B8%B8%E5%AF%84%EF%BC%81/">省选游记？省选游寄！</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      
      &copy; 2023 John Doe<br>
      Powered by <a href="https://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>

    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="/js/jquery-3.6.4.min.js"></script>



  
<script src="/fancybox/jquery.fancybox.min.js"></script>




<script src="/js/script.js"></script>





  </div>
</body>
</html>